// greetings.h : communications with PC terminal.
/*
 Required includes:	<stdint.h>
 					<string.h>
 					"usart.h"
 */

/*
 Copyright 2010 Anthony Oko, Koby Hitchen, Mara Cairo, Shaochen Xu, Tyler Lucas
 
 This file is part of the trajctrl program.
 
 trajctrl is licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
 http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

// put character arrays in flash

#include "greeting.h"

/*
 Interacts with user to get parameters.
 inputs:	*user		[2^5-1] char array pointer
			*date		[10] char array pointer
			*btype		[42] char array pointer
			*notes		[255] char array pointer, init'd to 0xFFFF...
			runid		run ID number
			bsize		bead size, in microns (500 or 1000)
			smask		stage bit mask
 outputs:	all inputs, by reference
 */
greeting_return greeting()
{
	greeting_return gr;
	uint8_t stagesf[8];

	struct text {
		const char line[] = "-------------------------------------------------------------";
		const char invip[] = "Invalid input.";
		const char re[] = "Restarting...";
		const char err[] = "Error!";
	} txt;
	
	// look for PC connection
	
	// get parameters
	txstr("Welcome to trajctrl.\n");
	txstr("\n\rUsername:          ");
	*(gr.user) = rxstr_EOF("\r",strlen(gr.user));
	txstr("Date (YYYY/MM/DD): ");
	*(gr.date) = rxstr_len(11);
	txstr("\n\rRun #:             ");
	*(gr.runid) = rxstr_EOF("\r",strlen(gr.runid));
	txstr("Bead Size:         ");
	*(gr.bsize) = rxstr_EOF("\r",strlen(gr.bsize));
	txstr("Bead Type:         ");
	*(gr.btype) = rxstr_EOF("\r",strlen(gr.btype));
	txstr("Notes:\n\r  ");
	*notes = rxstr_ESC(strlen(gr.notes), *notes);
	txstr("What stages would you like to use? You may type a list in the\n\r"
			"format \"1,2,4-6,9\", \"none\", or \"all\": ");
	char stages[11] = rxstr_EOF("\r",0);
	&(stagesf[0]) = str2uint8ar(&(stages[0]),ENDCHAR);
	gr.smask = uint8ar2bitmask(stagesf);
	txstr("\n\rStages "); tx('[');
	for(uint8_t i=0;i<((strlen(stages)+strlen(stages)%2)/2);tx(','))
		tx(uint82char(stagesf[i++]));
	tx(']');
	txstr(" initiated.");
	txstr("\n\r");
	txstr("\n\rPress Enter when ready to start.");
	txstr("\n\r");
	while (rx() != '\r')
		txstr("\n???\tPress Enter when ready to start.");
	txstr("Loading bead");
		
	return gr;
}

// parses string into uint8_t array
uint8_t *str2uint8ar(char *s, char c)
{
	uint8_t n[(strlen(s)+strlen(s)%2)/2];
	uint8_t i=0;
	uint8_t ni=0;
	
	do {
		if ((s[i]>'1') && (s[i]<'9'))
			n[ni++] = s[i]-'0';

		switch (s[i++]) {
			case ',':
				break;
			case '-':
				for(uint8_t ss=(s[i-2]-'0');ss<(s[i]-'0');ss++)
					n[ni++] = ss;
				break;
			default:
				ledmsg("...___...___...");
				break;
		}

	} while (s[++i] != c);
	return n;
}

// converts binary number to number character
char uint82char(uint8_t u)
{
	return (char)('0' + u);
}

// converts number character to binary number
uint8_t char2uint8(char c)
{
	return (uint8_t)(c - '0');
}

uint8_t uint8ar2bitmask(uint8_t u[8])
{
	uint8_t b = 0b00000000;
	for(uint8_t i=8;i;--i)
	{
		b |= (u[i]<<i);
	}
	return b;
}
